package com.weiyuwqj.server.service.invite.impl;

import com.github.pagehelper.PageHelper;
import com.weiyuwqj.server.service.sys.module.ConfigTicketModule;
import com.weiyuwqj.server.service.base.BaseServiceImpl;
import com.weiyuwqj.server.mapper.BasUserInfoMapper;
import com.weiyuwqj.server.mapper.BasUserInviteMapper;
import com.weiyuwqj.server.mapper.BusAccountBankMapper;
import com.weiyuwqj.api.service.BasUserInviteService;
import com.weiyuwqj.api.common.PageList;
import com.weiyuwqj.api.common.Paginator;
import com.weiyuwqj.api.constant.Constants;
import com.weiyuwqj.api.exception.CrowdException;
import com.weiyuwqj.api.model.BasUserInfo;
import com.weiyuwqj.api.model.BasUserInvite;
import com.weiyuwqj.api.model.BusAccountBank;
import com.weiyuwqj.api.model.TicketParamEntity;
import com.weiyuwqj.api.model.vo.*;
import com.weiyuwqj.api.util.IpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Yao xiuwei on 2015/12/2.
 */
@Service("userInviteService")
public class BasUserInviteServiceImp extends BaseServiceImpl<BasUserInvite> implements BasUserInviteService {
    @Autowired
    BasUserInviteMapper inviteMapper;

    @Autowired
    BasUserInfoMapper userInfoMapper;

    @Autowired
    ConfigTicketModule ticketModule;

    @Autowired
    BusAccountBankMapper bankMapper;

    //分页显示 & 查询
    @Override
    public PageList getList(Paginator p, Map<String, Object> param) {
        PageHelper.startPage(p.getPageNum(), p.getPageSize());
        return new PageList(inviteMapper.getList(param));
    }

    @Override
    public List<BasUserInviteVo> getList(Map<String, Object> param) {
        return inviteMapper.getList(param);
    }

    @Override
    public PageList getListByUserId(Paginator p, Integer userId) {
        PageHelper.startPage(p.getPageNum(), p.getPageSize());
        List<Map> list = inviteMapper.getListByUserId(userId);
        return new PageList(list);
    }

    @Override
    public BasUserInviteVo getOneByInviteId(Integer inviteId) {
        return inviteMapper.getOneByInviteId(inviteId);
    }

    @Override
    public Map inviteBinding(Integer userId, String inviteCode) {
        BasUserInfo userInfo = userInfoMapper.getByInviteCode(inviteCode);
        if (userInfo == null){
            throw new CrowdException("invite.invalid_invite_code");
        }
        BasUserInviteVo userInviteVo =inviteMapper.getOneByInviteId(userId);
        if (userInviteVo !=null){
            throw new CrowdException("invite.user_had_bound_invite_code");
        }
        if (userInfo.getUserId().intValue() == userId.intValue()){
            throw new CrowdException("invite.user_self_bound_invite_code");
        }
        //查看邀请人的邀请人
        BasUserInviteVo inviteVo =inviteMapper.getOneByInviteId(userInfo.getUserId());
        if (inviteVo !=null && inviteVo.getUserId().intValue() == userId.intValue()){
            throw new CrowdException("invite.user_invite_user_is_user");
        }
        BasUserInvite userInvite = new BasUserInvite();
        userInvite.setInviteId(userId);
        userInvite.setUserId(userInfo.getUserId());
        userInvite.setAddtime(new Date());
        userInvite.setAddip(IpUtils.get());
        Integer count = inviteMapper.insert(userInvite);

        BusAccountBank bank = bankMapper.getOneByUserId(userId);
        if(bank != null){
            TicketParamEntity realNameParam = new TicketParamEntity();
            realNameParam.setNid(Constants.realname_invite_nid);
            realNameParam.setUserId(userInfo.getUserId());
            ticketModule.addTicket(realNameParam);
        }

        //邀请人获得奖励
        TicketParamEntity investParam = new TicketParamEntity();
        investParam.setNid(Constants.invite_nid);
        investParam.setUserId(userInfo.getUserId());
        ticketModule.addTicket(investParam);

        Map map = new HashMap();
        map.put("result","success");
        return map;
    }

    @Override
    public void modifyInvite(Map map) {
        inviteMapper.modifyInvite(map);
    }
}


